import axios from "/client/axios.js"

// 验证是否已登陆
export default function () {
  return new Promise(res => {
    // 获取本地的用户信息和token
    let user = localStorage.getItem("user")
    let token = localStorage.getItem("token")
    if (!user || !token) {
      return res({
        state: 0,
        message: "您未登录,请先登录"
      })
    }
    user = JSON.parse(user)
    token = JSON.parse(token)
    axios.post("/user/info", { user, token }).then(({ data }) => {
      if (data.code != 1) {
        return res({
          state: 0,
          message: "token失效,请重新登录"
        })
      } else {
        return res({
          state: 1,
          message: "已经登录",
          data: data.data
        })
      }
    })
  })
}